#include "XPlay2.h"
#include <QtWidgets/QApplication>
#include "XDemux.h"
#include <QDebug>
#include "XDecode.h"
#include "XResample.h"
#include <QThread>
#include "XAudioPlay.h"
#include "XAudioThread.h"
#include "XVideoThread.h"
using namespace std;

class TestThread :public QThread
{
public:
	XAudioThread at;
	XVideoThread vt;
	XDemux demux;
	//XDecode vdecode;
	//XDecode adecode;
	XVideoWidget *video = 0;
	//XResample resample;
	void Init()
	{
		//char *url = "qqtest.mp4";
		qDebug() << "demux open =" << demux.Open("qqtest.mp4");
		/*demux.Read();
		demux.Clear();
		demux.Close();*/
		//qDebug() << "demux CopyPara" << demux.CopyVPara();
		//qDebug() << "demux CopyAPara" << demux.CopyAPara();
		//qDebug() << "seek = " << demux.Seek(0.9);
		
		//qDebug() << "vdecode.Open() =" << vdecode.Open(demux.CopyVPara());
		//vdecode.Clear();
		//vdecode.Close();
		
		/*qDebug() << "adecode.Open() =" << adecode.Open(demux.CopyAPara());
		qDebug() << "resample.Open() =" << resample.Open(demux.CopyAPara());*/
		/*XAudioPlay::Get()->channels = demux.channels;
		XAudioPlay::Get()->sampleRate = demux.sampleRate;
		qDebug() << "XAudioPlay::Get()->Open = " << XAudioPlay::Get()->Open();*/
		qDebug() << "at.Open =";
		
		qDebug()<< at.Open(demux.CopyAPara(), demux.sampleRate, demux.channels);
		vt.Open(demux.CopyVPara(), video, demux.width, demux.height);
		at.start();
		vt.start();
		//!decode->Open(para)
	}
	//unsigned char *pcm = new unsigned char[1024 * 1024*10];
	void run()
	{
		for (;;) {
			AVPacket *pkt = demux.Read();
			if (demux.IsAudio(pkt) == true) {
				at.Push(pkt);
				
			} else {
				vt.Push(pkt);
				//qDebug() << "Video:" << frame;
			}
			if (pkt == NULL) {
				demux.Seek(0);
			}
		}
	}
	
};
#include "XDemuxThread.h"
int main(int argc, char *argv[])
{
	
	//初始化显示
	
	
    QApplication a(argc, argv);
    XPlay2 w;
    w.show();
	/*XDemuxThread dt;
	char *url = "rtmp://58.200.131.2:1935/livetv/cetv1";
	dt.Open("qiangdaotuan.flv", w.ui.video);
	dt.Start();*/
	
    return a.exec();
}
